/*
 * @lc app=leetcode id=443 lang=cpp
 *
 * [443] String Compression
 */

// @lc code=start
class Solution {
public:
    int compress(vector<char>& chars) {
 int j = 0;
        int ans ;
        std::string  res;
 
        for (int i = 0 ; i < chars.size(); ++i ) {
            if (chars[j] != chars[i]) {
                if (i - j >= 2) {
                    res.push_back(chars[j]);
                    res += std::to_string(i -j);
                } else {
                  res.push_back(chars[j]);
                      
                }
                j = i;
            }
        }
        if (chars.size() - j >= 2) {
             res.push_back(chars[j]);
            res += std::to_string(static_cast<int>(chars.size()) - j);
        } else {
              res.push_back(chars[j]);
           
        }
        chars.clear();
        for (auto r : res) {
            chars.push_back(r);
        }
        
        return res.size();

    }
};
// @lc code=end

